#include <git2.h>
#include <stdio.h>
#include <string.h>

typedef struct {int e;} walk_data;

int walk_cb(const char *root,
	            const git_tree_entry *entry,
	            void *payload)
{
  walk_data *d = (walk_data*)payload;
  /* … */
  char buf[GIT_OID_HEXSZ+1];
  bzero(buf, GIT_OID_HEXSZ+1);
  const git_oid *oid = git_tree_entry_id(entry);
  git_oid_fmt(buf, oid);
  printf("(%s,%s,%d,%d)\n", git_tree_entry_name(entry), buf, git_tree_entry_filemode(entry), git_tree_entry_type(entry));
}

int main() {
	git_libgit2_init();
	char *repo_path = "test_repos";
	git_repository *repo;
	int ret = git_repository_open(&repo ,repo_path);
	if(ret<0) {
		printf("git_repository_open failed, error msg:%s\n", giterr_last()->message);
		return -1;
	}
	printf("repository.path:%s\n", git_repository_path(repo));


	// printf("find repo path:%s\n", path.ptr);

	// git_reference *head;
	// git_repository_head(&head, repo);
	// const git_oid *oid = git_reference_target(head);
	// git_commit *commit = NULL;
	// git_commit_lookup(&commit, repo, oid);
	// git_tree *tree = NULL;
	// git_commit_tree(&tree, commit);

	git_object *obj = NULL;
	int error = git_revparse_single(&obj, repo, "HEAD^{tree}");
	git_tree *tree = (git_tree *)obj;

	walk_data d = {0};
	error = git_tree_walk(tree, GIT_TREEWALK_PRE, walk_cb, &d);

	return 0;
}
